home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / visulztn / saoimage / saoimage.lha / ctrlfile.c < prev    next >
C/C++ Source or Header  |  1990-04-20  |  7KB  |  230 lines

  1. #ifndef lint
  2. static char SccsId[] = "%W%  %G%";
  3. #endif
  4.  
  5. /* Module:    ctrlfile.c (Control File)
  6.  * Purpose:    Prompt for file name and open file for specified purpose
  7.  * Subroutine:    open_output_file()
  8.  * Subroutine:    open_input_file()
  9.  * Subroutine:    swap_bytes()
  10.  * UNIX calls:    fopen(), lstat(), unlink(), extern int errno
  11.  * Copyright:    1989 Smithsonian Astrophysical Observatory
  12.  *        You may do anything you like with this file except remove
  13.  *        this copyright.  The Smithsonian Astrophysical Observatory
  14.  *        makes no representations about the suitability of this
  15.  *        software for any purpose.  It is provided "as is" without
  16.  *        express or implied warranty.
  17.  * Modified:    {0} Michael VanHilst    initial version          9 July 1989
  18.  *        {n} <who> -- <does what> -- <when>
  19.  */
  20.  
  21. #include <stdio.h>        /* FILE, stderr, NULL, etc. */
  22. #include <ctype.h>        /* macros for isupper, tolower, etc */
  23. #include <sys/types.h>        /* needed for lstat */
  24. #include <sys/stat.h>        /* needed for lstat, struct stat */
  25. #include <errno.h>        /* ENOENT */
  26. #include <X11/Xlib.h>        /* X window stuff */
  27. #include "hfiles/edit.h"    /* EditStruct */
  28.  
  29. #ifdef VMS
  30. #define lstat stat
  31. #define unlink delete
  32. #endif
  33.  
  34. /* May not get defined on some systems, redeclaration doesn't seem to hurt */
  35. extern int errno;
  36.  
  37. /*
  38.  * Subroutine:    open_output_file
  39.  * Purpose:    Open a file for writing, as specified by user
  40.  * Post state:    Sets pointer to file, open and ready for writing
  41.  * Returns:    2 = append, 1 = new file, 0 = user decided not to, -1 = error
  42.  */
  43. int open_output_file ( fd, edit, one_popup_row, prompt )
  44.      FILE **fd;
  45.      EditStruct *edit;
  46.      int one_popup_row;        /* i: put-promt-before-edit-line, else above */
  47.      char *prompt;
  48. {
  49.   int exist;
  50.   static EditStruct *instruct = NULL;
  51.   static char *response;
  52.   int reply;
  53.   char filename[128];
  54.   char open_type[4];
  55.   int done;                /* l: return status */
  56.   int get_edit_input();
  57.   EditStruct *init_edit_popup();
  58.   void clear_edit_buf(), unmap_popwin();
  59.   static int file_exists();
  60.  
  61.   open_type[0] = 0;
  62.   if( (get_edit_input(edit, one_popup_row, 1, 0, prompt) <= 0) ||
  63.       (edit->char_cnt == 0) ) {
  64.     unmap_popwin();
  65.     return( 0 );
  66.   }
  67.   strncpy(filename, edit->string, edit->char_cnt);
  68.   filename[edit->char_cnt] = '\0';
  69.   /* check if file already exists */
  70.   exist = file_exists(filename);
  71.   done = -3;
  72.   if( exist >0 ) {
  73.     /* if it exists, get instructions on how to respond */
  74.     if( instruct == NULL ) {
  75.       instruct = init_edit_popup(NULL, 8);
  76.       response = instruct->string;
  77.     }
  78.     do {
  79.       clear_edit_buf (instruct);
  80.       reply =
  81.     get_edit_input(instruct, 1, 0, 0,
  82.                "Enter 'o' (overwrite), 'a' (append), 'q' (quit): ");
  83.       if( reply <= 0 ) {
  84.         done = 0;
  85.       } else if( instruct->char_cnt > 0 ) {
  86.     if( isupper(response[0]) )
  87.       response[0] = tolower(response[0]);
  88.     /* quit, do nothing */
  89.     if( response[0] == 'q') {
  90.       /* quit */
  91.       done = 0;
  92.     } else if( response[0] == 'o') {
  93.       /* overwrite existing file */
  94.       strcpy(open_type, "w");
  95.       if( unlink(filename) <0 ) {
  96.         (void)fprintf(stderr, "Error: can't delete file\n");
  97.         perror (filename);
  98.         done = -1;
  99.       } else
  100.         done = 1;
  101.     } else if( response[0] == 'a') {
  102.       /* append to end of existing file */
  103.       strcpy(open_type, "a");
  104.       done = 2;
  105.     } else
  106.       /* else unknown response */
  107.       XBell(edit->display, 20);
  108.       }
  109.     } while( done == -3 );
  110.   } else if( exist ==0 ) {
  111.     /* file does not yet exist */
  112.     strcpy(open_type, "a");
  113.     done = 1;
  114.   } else {
  115.     /* problem accessing file */
  116.     (void)fprintf(stderr, "Error attempting to access file\n");
  117.     perror(filename);
  118.     done = -1;
  119.   }
  120.   unmap_popwin();
  121.   if( done > 0 ) {
  122.     /* open the file appropriately */
  123.     if( (*fd=fopen(filename, open_type)) == NULL ) {
  124.       (void)fprintf(stderr, "Error attempting to open file\n");
  125.       perror(filename);
  126.       done = -1;
  127.     }
  128.   }
  129.   if( done < 0 )
  130.     XBell(edit->display, 20);
  131.   return( done );
  132. }
  133.  
  134. /*
  135.  * Subroutine:    open_input_file
  136.  * Purpose:    Open a file for reading, as specified by user
  137.  * Note:    Sets pointer of file open and ready for reading
  138.  * Returns:    1 on success, 0 if user decided not to, -1 on error
  139.  */
  140. int open_input_file ( fd, edit, one_popup_row, prompt )
  141.      FILE **fd;
  142.      EditStruct *edit;
  143.      int one_popup_row;        /* i: put-promt-before-edit-line, else above */
  144.      char *prompt;
  145. {
  146.   char filename[132];
  147.   int exist;
  148.   int get_edit_input();
  149.   static int file_exists();
  150.  
  151.   if( get_edit_input(edit, one_popup_row, 1, 1, prompt) <= 0 )
  152.     return( 0 );
  153.   strncpy(filename, edit->string, edit->char_cnt);
  154.   filename[edit->char_cnt] = '\0';
  155.   /* see if it exists */
  156.   exist = file_exists(filename);
  157.   if( exist ==0 ) {
  158.     (void)fprintf(stderr, "Error: file %s does not exist\n", filename);
  159.     return( -1 );
  160.   } else if( exist <0 ) {
  161.     (void)fprintf(stderr, "Error attempting to access file\n");
  162.     perror(filename);
  163.     return( -1 );
  164.   }
  165.   /* open the cursor file */
  166.   if( (*fd = fopen(filename, "r")) == NULL ) {
  167.     (void)fprintf(stderr, "Error attemping to open file\n");
  168.     perror(filename);
  169.     return( -1 );
  170.   }
  171.   return( 1 );
  172. }
  173.  
  174.  
  175. /*
  176.  * Subroutine:    file_exists
  177.  * Purpose:    does a file exist for opening (1=yes, 0=no, -1=problem)
  178.  */
  179. static int file_exists ( name )
  180.      char *name;
  181. {
  182.   int i;
  183.   struct stat buf;
  184.  
  185.   i = lstat(name, &buf);
  186.   if( i ==0 )
  187.     return(1);            /* found the file */
  188.   else if( errno == ENOENT )
  189.     return(0);            /* didn't find the file */
  190.   else
  191. #ifndef VMS
  192.     return(-1);           /* a real error of some sort */
  193. #else
  194.     return(0);          /* VMS doesn't set errno properly in this case */
  195. #endif
  196. }
  197.  
  198.  
  199. /*
  200.  * Subroutine:    swap_bytes
  201.  * Purpose:    Swap bytes in place, (swab is not guaranteed to work in place)
  202.  */
  203. void swap_bytes ( array, nbytes )
  204.      char *array;
  205.      int nbytes;
  206. {
  207.   register char *low_byte, *high_byte, *last_byte;
  208.   register unsigned temp;
  209.   register int next=2;
  210.  
  211.   /* swap successive pairs of bytes */
  212.   low_byte = array;
  213.   high_byte = array+1;
  214.   last_byte = array + nbytes;
  215.   while( high_byte < last_byte ) {
  216.     temp = *low_byte;
  217.     *low_byte = *high_byte;
  218.     *high_byte = temp;
  219.     /* there is no hardware incr on the 68000, so why bother with ++? */
  220.     low_byte += next;
  221.     high_byte += next;
  222.   }
  223. }
  224.                          
  225.                                                                
  226.                                                                
  227.                                                                
  228.                                                                
  229.                                        
  230.